
var prefix = ctx +"module/department"
$(function() {
    load();
});
var form, table, element;
function load(){
    layui.use(['form','table'], function() {
        form = layui.form;
        table = layui.table;
        element = layui.element;
//载入table模块
        form.on('select(townId)', function (data) {
            townIdChange(data.value,form)
        });
        var searchParams = {};
        searchParams['keyword']=$('#keyword').val();
        searchParams['townId']=$('#townId').val();
        searchParams['companyId']=$('#companyId').val();
        //初始化table
        table.render({
            elem: '#exampleTable',//承载数据的table Id
            url: prefix + "/list",//请求数据连接
            toolbar: '#toolbarDemo',
            cellMinWidth: 80,//全局定义所有常规单元格的最小宽度（默认：60）
            even: true,//是否开启隔行变色
            loading:true,//是否显示加载条
            // height: 'full-75',
            where :{
                searchParams:searchParams,
                sorts:{"id":"desc"}
            },
            //page: true,//是否分页
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: [ 'prev', 'page', 'next', 'skip', 'count','limit'] //自定义分页布局
            },
            cols: [[
                {type:'numbers',title: '序号', width:'5%'},
                { field : 'name',  title : '部门名称',  align : 'center', },
                { field : 'functions',  title : '部门职责', align : 'center', },
                { field : 'headman',  title : '负责人', align : 'center',   },
                { field : 'deptType',  title : '部门类型', align : 'center',
                    templet : function(value) {
                        if (value.deptType == 0) {
                            return '<span class="label label-primary">普通部门</span>';
                        } else if (value.deptType == 1) {
                            return '<span class="label label-success">安全科</span>';
                        } else if (value.deptType == 2) {
                            return '<span class="label label-warning">电焊科</span>';
                        }  else if (value.deptType == 3) {
                            return '<span class="label label-warning">高空作业部</span>';
                        } else {
                            return '<span class="label label-warning">'+ value.deptType + '</span>';
                        }
                    }
                },
                { field : 'tel',  title : '联系方式', align : 'center',},
                { title: '操作', minWidth: 50, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            id: 'currentTableId'//表格Id
        });
        /**
         * 操作栏tool  监听事件
         */
        table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === 'edit') {  // 监听添加操作
                console.log('obj',obj.data.id)
                edit(obj.data.id);
            } else if (obj.event === 'delete') {  // 监听删除操作
                console.log('obj',obj.data.id)
                remove(obj.data.id);
            }
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            reLoad();
            return false;
        });

        /**
         * 操作栏tool  监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                add();
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            } else if (obj.event === 'back') {  // 监听删除操作
                goBack();
            }
        });
    });
}
function townIdChange(townId) {
    $.ajax({
        type: "POST",
        url: ctx +"module/company/getList",
        data: {
            townId: townId
        },
        success: function (data) {
            if (data.code == 0) {
                var shopList = data.data;
                $("#companyId").html("");
                if (shopList.length > 0) {
                    $("#companyId").append("<option value=\"\" selected=\"selected\">所属企业</option>");
                    for (var index in shopList) {
                        var str = "<option value=\"" + shopList[index].id + "\" >" + shopList[index].shopName + "</option>";
                        $("#companyId").append(str);
                    }
                } else {
                    var str = "<option value=\"\" selected=\"selected\">----无----</option>";
                    $("#companyId").append(str);
                }
                form.render("select");
            }
        }
    });
}



function goBack(){
    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
    parent.layer.close(index);
}



function reLoad() {
    var searchParams = {};
    searchParams['keyword']=$('#keyword').val();
    searchParams['townId']=$('#townId').val();
    searchParams['companyId']=$('#companyId').val();
    layui.use(['table'], function() {
        var  page = $(".layui-laypage-skip").find("input").val() //当前页码值
        console.log('page',page)
        var table = layui.table;//载入table模块
        table.reload('currentTableId', {
            page: {curr: page }
            , where: {
                searchParams: searchParams
            }
        }, 'data');
    });
}


function add() {
    var companyId = $("#companyId").val()
	var index = layer.open({
        type : 2,
        title : '增加',
        maxmin : true,
        shadeClose : false, // 点击遮罩关闭层
        area : [ '650px', '550px' ],
        content : prefix + '/add/' + companyId // iframe的url
    });
	// layer.full(index);
}

function edit(id) {
	var index = layer.open({
        type : 2,
        title : '编辑',
        maxmin : true,
        shadeClose : false, // 点击遮罩关闭层
        area : [ '650px', '550px' ],
        content : prefix + '/edit/' + id // iframe的url
    });
	// layer.full(index);
}
function remove(id) {
    layer.confirm('确定要删除选中的记录？', {
        btn : [ '确定', '取消' ]
    }, function() {
        $.ajax({
            url : prefix+"/remove",
            type : "post",
            data : {
                'id' : id
            },
            success : function(r) {
                if (r.code==0) {
                    layer.msg(r.msg);
                    reLoad();
                }else{
                    layer.msg(r.msg);
                }
            }
        });
    })
}


